// http://api.douban.com/v2/user/1000001
const rq = require('request-promise-native')
const mongoose = require("mongoose")
const Movie = mongoose.model("Movie")
const Category = mongoose.model("Category")


async function fetchMovie (item) {
    const url = `http://api.douban.com/v2/movie/${item.doubanId}`
    const res = await rq(url)
    let body
    try{
        body = JSON.parse(res)
    }catch(err){
        console.log(err)
    }
    return body
}

;(async ()=>{
    let movies = await Movie.find({
        $or: [
            {summary: {$exists: false}},
            {summary: null},
            {year: {$exists: false}},
            {title: ''},
            {summary: ''}
        ]
    })

    for (let i=0,len=movies.length; i<len;i++){
        let movie = movies[i]
        let movieData = await fetchMovie(movie)

        if (movieData){
            let tags = movieData.tags
            movie.tags = movie.tags || []
            movie.summary = movieData.summary || ''
            movie.title = movieData.alt_title || movieData.title || ''
            movie.rawTitle = movieData.title || movieData.alt_title || ''

            if (movieData.attrs){
                movie.movieTypes = movieData.attrs.movie_type || []
                movie.year = movieData.attrs.year[0] || 2500
                for (let j=0; j<movie.movieTypes.length;j++){
                    let item = movie.movieTypes[j]
                    let cat = await Category.findOne({
                        name: item
                    })
                    if (!cat){
                        cat = new Category({
                            name: item,
                            movies: [movie._id]
                        })
                    } else {
                        if (cat.movies.indexOf(movie._id)===-1){
                            cat.movies.push(movie._id)
                        }
                    }
                    await cat.save()

                    if (!movie.category){
                        movie.category.push(cat._id)
                    } else {
                        if (movie.category.indexOf(cat._id)===-1){
                            movie.category.push(cat._id)
                        }
                    }
                }

                let dates = movieData.attrs.pubdate || []
                let pubdates = []

                dates.map(item=>{
                    if (item && item.split('(').length>0) {
                        let parts = item.split("(")
                        let date = parts[0]
                        let country = '未知'

                        if (parts[1]){
                            country = parts[1].split(")")[0]
                        }

                        pubdates.push({
                            date: new Date(date),
                            country
                        })

                    }
                })

                movie.pubdate = pubdates

            }
            tags.forEach(tag=>{
                movie.tags.push(tag.name)
            })
            // console.log(movie)
            await movie.save()
        }
    }

})()


/*
    异步非阻塞
        从主动轮询变成被动通知
        按下开关就是注册回调函数，通知执行回调
    实现异步
        主动轮询，被动通知执行回调
    
*/ 